import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:laoba_menu/views/product-detail/MaterialItem.dart';
import 'package:laoba_menu/views/product-detail/StepItem.dart';
import 'Data.dart';
import 'Material.dart';
import 'Step.dart';

class CookDetail extends StatefulWidget {
  CookDetail({
    Key key,
    @required this.id,
    @required this.cover,
    @required this.name,
    @required this.desc,
  }) : super(key: key);
  final int id;
  final String cover;
  final String name;
  final String desc;
  @override
  CookDetailState createState() =>
      new CookDetailState(this.id, this.cover, this.name, this.desc);
}

class CookDetailState extends State<CookDetail> {
  int id;
  String cover;
  String name;
  String desc;
  CookDetailState(this.id, this.cover, this.name, this.desc);
  Dio dio = Dio();
  List stepList = [];
  List materialList = [];

  Future getData() async {
    final String url = 'http://127.0.0.1:7001/cook/findById';
    final res = await dio.get(url, queryParameters: {'id': this.id});
    var data = jsonDecode(res.toString());
    materialList = data['data']['material']
        .map((e) => MaterailsType.fromJson(e.cast<String, dynamic>()))
        .toList();
    stepList = data['data']['step']
        .map((i) => StepsType.fromJson(i.cast<String, dynamic>()))
        .toList();

    setState(() {});
  }

  @override
  initState() {
    super.initState();
    getData();
  }

  List<Widget> getMaterialWidgetList() {
    return materialList
        .map((item) => MaterialItem(
              name: item.name,
              id: item.id,
              number: item.number,
              cook_id: item.cook_id,
            ))
        .toList();
  }

  List<Widget> getStepWidgetList() {
    return stepList
        .asMap()
        .keys
        .map((value) => StepItem(
              name: stepList[value].name,
              id: stepList[value].id,
              desc: stepList[value].desc,
              cover: stepList[value].cover,
              index: value + 1,
            ))
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    double mediaWidth = MediaQuery.of(context).size.width; // 屏幕宽度
    return Scaffold(
        body: ListView(
      padding: const EdgeInsets.all(0),
      children: [
        Container(
            child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            GestureDetector(
              child: Image(
                image: NetworkImage(this.cover),
                fit: BoxFit.cover,
                width: mediaWidth,
                height: 300,
              ),
              onTap: () => Navigator.pop(context),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(20, 20, 20, 20),
              child: Flex(
                direction: Axis.horizontal,
                children: [
                  Expanded(
                      flex: 0,
                      child: Padding(
                          padding: const EdgeInsets.fromLTRB(0, 0, 20, 0),
                          child: ClipOval(
                            child: Image(
                              image: AssetImage("images/2.jpg"),
                              fit: BoxFit.cover,
                              width: 100,
                              height: 100,
                            ),
                          ))),
                  Expanded(
                      flex: 1,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            "小飘飘",
                            textAlign: TextAlign.left,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              color: Colors.black,
                              fontSize: 20.0,
                              height: 2,
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                            child: Text(
                              "唯有美食不可辜负",
                              textAlign: TextAlign.left,
                              maxLines: 1,
                              style: TextStyle(
                                  fontSize: 13.0,
                                  color: Colors.grey,
                                  height: 1.0),
                            ),
                          )
                        ],
                      ))
                ],
              ),
            ),
            Container(
                padding: const EdgeInsets.fromLTRB(20, 0, 20, 10),
                child: Text(
                  name,
                  style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold),
                )),
            Container(
                margin: const EdgeInsets.fromLTRB(20, 0, 20, 20),
                width: mediaWidth - 40,
                child: Text(
                  desc,
                )),
            Container(
              padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
              child: Text(
                "材料",
                style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              ),
            ),
            Container(
                decoration: new BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(10.0)),
                  color: Color.fromRGBO(240, 240, 240, 1),
                ),
                margin: const EdgeInsets.fromLTRB(20, 10, 20, 20),
                child: Column(
                  children: getMaterialWidgetList(),
                )),
            Container(
              padding: const EdgeInsets.fromLTRB(20, 10, 20, 10),
              child: Text(
                "步骤",
                style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              ),
            ),
            Container(
                child: Column(
              children: getStepWidgetList(),
            ))
          ],
        )),
      ],
    ));
  }
}
